<template>
  <el-dialog
    :title="dialist.isAdd ? '添加秒杀' : '编辑秒杀'"
    :visible.sync="dialist.isShow"
    center
    :before-close="cancle"
  >
    <el-form :model="list" ref="diaform" :rules="rules">
      <el-form-item :label-width="formLabelWidth" label="活动名称" prop="title">
        <el-input v-model="list.title" placeholder="请输入活动名称"></el-input>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="活动期限">
        <el-date-picker
          v-model="timers"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="一级分类">
         <el-select v-model="list.first_cateid" placeholder="请选择分类" @change="fidChange(list.first_cateid,false)">
           <el-option v-for="item in firstarr" :key="item.id" :label="item.catename" :value="item.id"></el-option>
         </el-select>
      </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="二级分类" >
         <el-select v-model="list.second_cateid" placeholder="请选择分类" @change="sidChange(list.second_cateid,false)">
           <el-option v-for="item in secondarr" :key="item.id" :label="item.catename" :value="item.id"></el-option>
         </el-select>
      </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="商品">
         <el-select v-model="list.goodsid" placeholder="请选择商品">
           <el-option v-for="item in goodsarr" :key="item.id" :label="item.goodsname" :value="item.id"></el-option>
         </el-select>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="list.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" v-if="dialist.isAdd" @click="add"
        >添 加</el-button
      >
      <el-button type="primary" v-else @click="edit">编 辑</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSeck, getSeckOne, editSeck,getCatelist,listGoods} from "../../util/axios";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      list: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      //获取时间信息
      timers:[],
      //获取一级分类
      firstarr:[],
      //获取二级分类
      secondarr:[],
      //获取商品
      goodsarr:[],
      formLabelWidth: "120px",
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  props: ["dialist"],
  methods: {
    //一级分类改变时触发，获取二级分类
    fidChange(id,n){
        getCatelist({pid:id}).then(res=>{
          if(res.code==200){
            this.secondarr = res.list?res.list:[];
            if(!n){
              this.list.second_cateid ="";
              this.list.goodsid = "";
            }
          }
        })
    },
    sidChange(id,n){
       listGoods({fid:this.list.first_cateid,sid:id}).then(res=>{
           if(res.code==200){
             this.goodsarr = res.list?res.list:[];
             if(!n){
               this.list.goodsid = "";
             }
           }else{
             this.$message.error(res.msg)
           }
       })
    },
    ...mapActions({
      getSecklist: "seck/getSecklistAction",
    }),
    //超出限制后的提示
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    //点击取消后隐藏dialog
    cancle() {
      //隐藏dialog
      this.$emit("cancle", false);
      //清空表格
      this.list = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      //清空验证
      this.$refs.diaform.resetFields();
      this.timers=[];
      // this.firstarr=[];
      this.secondarr=[];
      this.goodsarr=[];
    },
  
    //添加功能
    add() {
      this.$refs.diaform.validate((val) => {
        if (val) {
          this.list.begintime = new Date(this.timers[0]).getTime();
          this.list.endtime = new Date(this.timers[1]).getTime();
          addSeck(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getSecklist();
                this.cancle();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    //dialog的编辑
    edit() {
      this.$refs.diaform.validate((val) => {
        if (val) {
          editSeck(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getSecklist();
                this.cancle();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    lookup(id) {
      getSeckOne({ id }).then((res) => {
        if (res.code == 200) {
          this.list = res.list;
          this.list.id = id;
          this.fidChange(this.list.first_cateid,true);
          this.sidChange(this.list.second_cateid,true);
          let time1 = new Date(Number(this.list.begintime));
          let time2 = new Date(Number(this.list.endtime));
          this.timers = [time1,time2];
          // console.log(this.timers);
          //绑定属性可以回显 图片
        }
      });
    }

  },
  mounted(){
     getCatelist({pid:0}).then(res=>{
        if(res.code == 200){
          this.firstarr  = res.list?res.list:[];
        }
     });
  }
};
</script>

<style lang="stylus" scoped></style>